package com.example.rabbitmqstudy.spring;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    public static final String TOPIC_EXCHANGE = "topic_exchange";

    public static final String QUEUE = "queue_name";

    /**
     * @brief:  声明exchange，名称是itemTopicExchange
     */
    @Bean("itemTopicExchange")
    public Exchange topicExchange() {
        return ExchangeBuilder.topicExchange(TOPIC_EXCHANGE).durable(true).build();
    }

    /**
     * @brief:  声明queue，名称是itemQueue
     */
    @Bean("itemQueue")
    public Queue itemQueue() {
        return QueueBuilder.durable(QUEUE).build();
    }

    /**
     * @brief:  绑定itemQueue到itemTopicExchange
     */
    @Bean
    public Binding itemQueueExchange(@Qualifier("itemQueue") Queue queue,
                                     @Qualifier("itemTopicExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("item.#").noargs();
    }
}
